import {
  Body,
  Controller,
  Get,
  HttpException,
  HttpStatus,
  Param,
  Post,
  Query,
} from '@nestjs/common';
import { CatesService } from './cats.service';
import { Cat } from './cat.interface';

@Controller('cats')
export class CatsController {
  constructor(private catsService: CatesService) {
    this.catsService = catsService;
  }

  @Get('findAll')
  demoFind(): string {
    function sealed(target) {
      console.log('constructor', target);
      target.sealed = '烙上我的痕迹 哼哼哼';
      Object.seal(target);
      Object.seal(target.prototype);

      return target;
    }

    @sealed
    class Greeter {
      greeting;
      constructor(message) {
        this.greeting = message;
      }
      greet() {
        return 'Hello, ' + this.greeting;
      }
    }

    const momo = new Greeter('you hello');

    console.log('momo', momo);

    delete momo.greeting;

    console.log('momo', momo);

    return 'This action returns all cats';
  }

  @Get('demoGreeter')
  demoGreeter(): string {
    function classDecorator<T extends { new (...args: any[]): any }>(
      constructor: T,
    ) {
      return class extends constructor {
        newProperty = 'new property';
        // hello = 'override';
      };
    }

    @classDecorator
    class Greeter {
      property = 'property';
      hello: string;
      constructor(m: string) {
        this.hello = m;
      }
    }

    const momo = new Greeter('momo');

    console.log('momo', momo);

    return 'demo1';
  }

  @Get('create')
  async create(@Query() q: string): Promise<Cat[]> {
    return this.catsService.create({ name: q, age: 2, breed: '拉斯维加斯' });
  }

  @Get(':id')
  async findAll(@Param('id') id: string): Promise<string> {
    throw new HttpException(
      { status: 7777, error: '嘻嘻嘻嘻' },
      HttpStatus.FORBIDDEN,
    );
    return `让我康康你的id: ${id}呀`;
  }

  @Post('create')
  async createPost(@Body() { age, breed, name }: Cat): Promise<Cat[]> {
    return this.catsService.create({ name, breed, age });
  }
}
